package com.nuoniu.sibanyun.mapper.crm;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nuoniu.sibanyun.entity.crm.CrmDefineField;
import com.nuoniu.sibanyun.mapper.provider.CrmDefineFieldMapperProvider;
import com.nuoniu.sibanyun.mapper.provider.SysLogMapperProvider;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 客户管理自定义字段 Mapper 接口
 * </p>
 *
 * @author baiqiang
 * @since 2020-06-15
 */
@Mapper
@Component
public interface CrmDefineFieldMapper extends BaseMapper<CrmDefineField> {

    /**
     * 导入系统字段数据
     * @author 小啊刚
     * @date 2020/6/16
     */
    @Insert("insert into crm_define_field(code,sys_name,name,company_id,company_name,source,type,data_type,val,sort,column_width,on_locked,on_off,on_fill,on_edit,on_export,is_search,on_app_off,on_app_fill,is_app_search,status,create_user_id,create_user_name) " +
            "select code,sys_name,name,#{companyId},#{companyName},source,type,data_type,val,sort,column_width,on_locked,on_off,on_fill,on_edit,on_export,is_search,on_app_off,on_app_fill,is_app_search,status,#{userId},#{realName} from crm_define_field where company_id = 1 and data_type = 1")
    void saveAll(Long companyId, String companyName, Long userId, String realName);

    /**
     * 保存内容
     * @author 小啊刚
     * @date 2020/6/16
     */
    @Update("update crm_define_field set val=#{val} where id=#{id}")
    void saveFieldVal(CrmDefineField saveFieldVal);


    @Select("SELECT MAX(sort) FROM crm_define_field WHERE company_id = #{companyId} AND source = #{source}")
    Integer getLastSort(@Param("companyId")Integer companyId,@Param("source")String source);

    /**
     * 查询字段列表
     * @author 小啊刚
     * @date 2020/6/22
     */
    @SelectProvider(type = CrmDefineFieldMapperProvider.class, method = "findList")
    List<CrmDefineField> findList(CrmDefineField field);

    /**
     * 获取导入字段
     * @author 小啊刚
     * @date 2020/6/30
     */
    @Select("select if(source='linkMan', concat('link_', code, '_', data_type), concat('crm_', code, '_', data_type)) 'key', if(source='linkMan',  concat('联系人.', `name`), `name`) `title` from crm_define_field " +
            "where company_id = #{companyId} and source in('crm', 'linkMan') and data_type in(1,2) and code != 'crmNo' order by source, data_type, id")
    List<Map<String, String>> getCrmImportFiled(Integer companyId);

    @Select("select  concat(#{source},'_', code, '_', data_type) 'key',  `name` as `title` from crm_define_field " +
            "where company_id = #{companyId} and source = #{source} and data_type in(1,2) and code != 'crmNo' order by source, data_type, id")
    List<Map<String, String>> getCrmImportFiledBySource(Integer companyId,String source);

    @Select("select * from crm_define_field " +
            " where company_id = #{companyId} and source = #{source} and data_type in(1,2) and code != 'crmNo' order by sort")
    List<CrmDefineField> getCrmDefineFieldList(String source, Integer companyId);

    boolean updateOnLockedFromSystem(@Param("dto") CrmDefineField crmDefineField);

    /**
     * 检查当前租户的自定义字段 是否有使用
     * @param id
     * @return
     */
    @Select(" SELECT COUNT(1) FROM ${tableName} tab  WHERE tab.company_id = #{companyId} AND tab.define_fields->'$.\"${id}\"' AND tab.`status` = 0")
    int countDefineFieldUsedNumber(String tableName,Integer companyId,Integer id);


}
